import React from 'react'

/**
 * 推断Antd组件类型
 */
const deduceAntdComponentType = (element: React.ReactElement) => {
  if (!element || !element.type) return null
  // Antd 组件通常有 displayName（如 "Input"、"Select"）
  const elementType = element.type as React.NamedExoticComponent
  return elementType.displayName || 'Input'
}

/**
 * 动态修改Antd组件
 */
export const injectAntdComponent = (element: React.ReactElement<any>) => {
  const componentType = deduceAntdComponentType(element)
  // 如果非Antd组件，则不修改
  if (!componentType) {
    return element
  }
  const defaultPlaceholder = componentType?.startsWith('Input')
    ? '请输入'
    : '请选择'

  // 如果组件已经有 placeholder，则不注入
  if (element.props?.placeholder || !defaultPlaceholder) {
    return element
  }

  const newProps = {
    placeholder: defaultPlaceholder,
  }
  return React.cloneElement(element, newProps)
}
